<template>
  <div>
    <!-- 选项卡 -->
    {{ selValue }}

    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <hr />

    <!-- 模态框先头后脚在中心 -->
    <el-button @click="openAddDialog" type="primary">新增</el-button>
    <!-- 先写头部 -->
    <el-dialog title="这是dialog标题" :visible.sync="dialogVisble">
      <el-form :model="formData" ref="form">
        <el-form-item label="问题内容" prop="questionContent">
          <el-input v-model="formData.questionContent"></el-input>
        </el-form-item>

        <el-form-item label="问题回答" prop="questionAnswer">
          <el-input v-model="formData.questionAnswer"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <!-- <el-input v-model="formData.status"></el-input> -->
          <el-select v-model="formData.status">
            <el-option
              v-for="(item, index) in options"
              :key="index"
              :label="item.lable"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <!-- 再写脚部分 -->
      <span slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button @click="confirm">确定</el-button>
      </span>
    </el-dialog>
    <!-- 
    分页条 -->
    <!-- @size-change='handleSizeChange'
    @current-chang'handleCurrentChange' -->
    <!-- layout='total,sizes,prev,pager,next,jumper'
      组件布局：分别代表总数页，每条多少页，前，中间页数内容，后退，前往页 -->
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 30, 50, 100]"
      :page-size="pageIndex"
      background
      layout="total,sizes,prev,pager,next,jumper"
      :total="400"
    >
    </el-pagination>
    {{ pageIndex }}
    {{ pageSize }} -->

    <!-- 表单 -->
    <el-form ref="searchForm" :model="searchFormData" inline>
      <el-form-item label="问题内容" label-width="80px" prop="questionContent">
        <el-input v-model="searchFormData.questionContent"></el-input>
      </el-form-item>
      <el-form-item label="状态" label-width="80px" prop="status">
        <el-input v-model="searchFormData.status"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getTableData"
          >搜索</el-button
        >
        <el-button type="primary" icon="el-icon-edit" @click="reset"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 练习:生成表格，进行增删改查 -->
    <el-table :data="TableData" border>
      <el-table-column label="问题ID" prop="questionId"></el-table-column>
      <el-table-column
        label="问题内容"
        prop="questionContent"
      ></el-table-column>
      <el-table-column label="问题回复" prop="questionAnswer"></el-table-column>
      <el-table-column label="状态" prop="status">
         <template slot-scope="scope">
                    {{scope.row.status | filterStatus(options)}}
                </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="updateBtn(scope.row)">编辑</el-button>
          <el-button type="text" @click="deleteBtn(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <!-- @size-change='handleSizeChange'
    @current-chang'handleCurrentChange' -->
    <!-- layout='total,sizes,prev,pager,next,jumper'
      组件布局：分别代表总数页，每条多少页，前，中间页数内容，后退，前往页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 30, 50, 100]"
      :page-size="pageSize"
      background
      layout="total,sizes,prev,pager,next,jumper"
      :total="total"
    >
    </el-pagination>
    {{ pageIndex }}
    {{ pageSize }}
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      selValue: "",
      options: [
        { label: "选项1", value: "1" },
        { label: "选项2", value: "2" },
        { label: "选项3", value: "3" },
        { label: "选项4", value: "4" },
        { label: "选项5", value: "5" },
      ],
      dialogVisble: false,
      TableData: [],
      dialogType: "",
      searchFormData: {
        questionContent: "",
        status: "",
      },
      formData: {
        questionAnswer: "",
        status: "",
        questionContent: "",
      },
      //当前在第几页
      pageIndex: 1,
      pageSize: 10,
      total: 0,
    };
  },
   filters: {
        filterStatus (value, options) {
            let index = options.findIndex(item => {
                return value === item.value
            })
            //如果下表为0，则显示'-----'
           if(index<0){
             return '----'
           }else{
             return options[index].label
           }
        }
    },
  mounted() {
    this.getTableData();
  },

  methods: {
    openAddDialog() {
      this.dialogType = "add";
      this.dialogVisble = true;
    },
    //展示表内容
    getTableData() {
      axios
        .get("http://127.0.0.1:3000/api/test/crud/list", {
          params: {
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            questionContent: this.searchFormData.questionContent,
            status: this.searchFormData.status,
          },
        })
        .then((res) => {
          console.log(res);
          this.TableData = res.data.data;
          //动态赋值分页total
          this.total = res.data.total;
        });
    },
    //清空至初始化
    reset() {
      this.$refs.searchForm.resetFields();

      this.getTableData();
    },
    //dialog里的新增表格和修改表格
    confirm() {
      if (this.dialogType === "add") {
        axios
          .post("http://127.0.0.1:3000/api/test/crud/add", {
            ...this.formData,
          })
          .then(() => {
            this.getTableData();
            this.$refs.form.resetFields();
            this.dialogVisble = false;
          });
      } else if (this.dialogType === "edit") {
        axios
          .put("http://127.0.0.1:3000/api/test/crud/update", {
            ...this.formData,
          })
          .then((res) => {
            console.log(res);
            this.dialogVisble = false;
            this.$refs.form.resetFields();
            this.getTableData();
          });
      }
    },
    //编辑表格
    updateBtn(row) {
      this.dialogType = "edit";
      this.dialogVisble = true;

      //等待数据操作完成后在执行（等待前面的el-dialog,el-form，渲染完成后在去初始化表单数据）
      this.$nextTick(() => {
        this.formData = Object.assign({}, row);
      });
    },
    //删除表格
    deleteBtn(row) {
      this.$confirm("确定删除嘛").then(() => {
        axios
          .delete("http://127.0.0.1:3000/api/test/crud/delete", {
            params: {
              questionId: row.questionId,
            },
          })
          .then(() => {
            this.getTableData();
          });
      });
    },

    //切换页码触发方法
    handleCurrentChange(value) {
      console.log("当前页码", value);
      this.pageIndex = value;

      this.getTableData();
    },
    //分页条数变动的时候触发方法
    handleSizeChange(value) {
      console.log("当前每页多少条", value);
      this.pageSize = value;

      this.getTableData();
    },
    //dialog里的取消表格
    cancel() {
      this.$refs.form.resetFields();
      this.dialogVisble = false;
    },
  },
};
</script>